/**
 * Malsha 2014/11/28
 */

ChequeConfirmation = new Object();

ChequeConfirmation.getCenterPanel = function() {

	Ext.Loader.setPath('Ext.ux', 'js/libs');

	Ext.Loader.setConfig({
		enabled : true
	});

	Ext.require([ 'Ext.ux.CheckColumn', 'Ext.ux.RowExpander' ]);

	var unconfirm_cheque_investments = Ext.create('Ext.data.Store', {
		fields : [ 'id', 'invenstmentCode', 'amount', 'status', 'customer', 'customer_code', 'customer_name', 'fund_id', 'confirmDate', 'unitPrice', 'noOfUnits', 'nameOfBank',
				'branch', 'chequeNo', 'chequeDate', 'fundcode' ]
	});

	grid = Ext.create('Ext.grid.Panel', {
		title : 'Investments',
		store : unconfirm_cheque_investments,
		height : 300,
		plugins : [ {
			ptype : 'rowexpander',
			rowBodyTpl : [ '<b>Fund:</b> {fundcode}</br>', '<b>Unit Price:</b> {unitPrice}</br>', '<b> No Of Units:</b> {noOfUnits}' ]
		} ],
		columns : [ {
			header : 'Customer Code',
			dataIndex : 'customer_code',
			width : 100
		}, {
			header : 'Investment Code',
			dataIndex : 'invenstmentCode',
			width : 100
		}, {
			header : 'Amount',
			dataIndex : 'amount',
			width : 125,
			flex : 1,
			xtype : 'numbercolumn',
			format : '0,000.0000',
			align : 'right'
		}, {
			header : 'Cheque No',
			dataIndex : 'chequeNo',
			width : 125,
			flex : 1
		}, {
			header : 'Cheque Date',
			dataIndex : 'chequeDate',
			width : 125,
			flex : 1
		}, {
			xtype : 'checkcolumn',
			header : 'Confirm',
			dataIndex : 'view',
			align : 'center',
			stopSelection : false,
		} ]
	});

	return formpanel = Ext.create('Ext.form.Panel', {

		title : 'Confirmed Investment',
		bodyPadding : 10,
		frame : true,
		defaults : {
			labelWidth : 110,
			anchor : '65%',
		},
		items : [ grid ],
		buttonAlign : 'center',
		buttons : [ {
			text : 'Cancel',
			handler : function() {
				App.Core.removeScreen();
			}
		}, {
			text : 'Reset',
			handler : function() {
				this.up('form').getForm().reset();
			}
		}, {
			text : 'Save',
			id : 'save',
			handler : function() {
				var form = this.up('form').getForm();
				if (form.isValid()) {
					var investments = [];
					unconfirm_cheque_investments.each(function(record) {
						if (record.get('view')) {
							var obj = {
								"invenstmentCode" : record.get("invenstmentCode"),
								"id" : record.get("id")
							};
							investments.push(obj);
						}
					});

					form.url = 'realisingCheque.action';
					form.submit({
						params : {
							"investments" : Ext.encode(investments)
						},
						success : function(form, action) {
							form.reset();
							Ext.Msg.alert('Success', 'Saved Successfully');
						},
						failure : function(form, action) {
							Ext.Msg.alert('Failed', action.result.resulteMsg);
						}
					});

				}
			}
		} ],
		listeners : {
			render : function(p) {
				Ext.Ajax.request({
					url : 'getunconfirmChequeInvestment.action',
					success : function(response) {
						var obj = Ext.decode(response.responseText);
						unconfirm_cheque_investments.loadRawData(obj.investmentBeans);
					}
				});
			}
		}
	});

};